//底部导航菜单
var footer = '';
footer += '<section class="weui-menu">';
footer += '<div class="weui-menu-inner">';
footer += '<a href="./ip_apply.html"><em class="icon icon-27 other"></em><span>提交保单</span></a>';
footer += '</div>';
footer += '<div class="weui-menu-inner">',
footer += '<em></em> ';
footer += '<span>我的</span>';
footer += '<ul id="menuUl">';
footer += '<li><a href="./ip_my_apply.html">我的申请</a></li>';
footer += '<li><a href="./information_my.html">我的信息</a></li>';
footer += '</ul>';
footer += '</div>';
footer += '</section>';
$('footer').append(footer);

$('.weui-menu').find('.weui-menu-inner').click(function (e) {
    var menu = $(this).find('ul'),
        height = menu.find('li').length * 40 + 10 + 'px',
        opacity = menu.css('opacity');
    $('.weui-menu-inner ul').css({
        'top': '50px',
        'opacity': 0,
        'left': 0,
        'right': 0
    });
    if(opacity == 0) {
        menu.css({
            'top': '-' + height,
            'opacity': 1
        });
    }else {
        menu.css({
            'top': '50px',
            'opacity': 0
        });
    }

    $(document).one("click", function(){
        $("#menuUl").css({
            'top': '50px',
            'opacity': 0
        });
    });

    e.stopPropagation();
});

$("#menuUl").on("click", function(e){
    e.stopPropagation();
});

//是否认证
function isAuth() {
    var str = window.location.pathname;
    var fileName = str.substr(str.lastIndexOf("/")+1);

    $.ajax({
        url: url + 'weixin/userIfAuth.do',
        type: "POST",
        data:{
            "openId": openId
        },
        cache:false,
        success:function(data){
            //console.log(data);
            if(data.repCode = "000000"){
                if(data.data.authFlag == 0){  //未认证
                    if(fileName === "information_apply.html"){
                        console.log(fileName);
                    }else {
                        window.location.href = "information_apply.html";
                    }
                }else if(data.data.authFlag == 1){  //已认证
                    if(fileName === "information_apply.html"){
                        window.location.href = "information_my.html";
                    }else {
                        console.log(fileName);
                    }
                    console.log("已认证");
                }else if(data.data.authFlag == 3){  //审核中
                    if(fileName === "information_apply_audit_auth.html"){
                        console.log(fileName);
                    }else {
                        window.location.href = "information_apply_audit_auth.html";
                    }
                }
            }
        },
        error:function(){
            mui.toast("error,userIfAuth");
        }
    });
}
$(function () {
    setTimeout(function () {
        isAuth();
    },200);
})

//保险公司代码转换
function  companyCodeConvert(val) {
    if(val == "TABX"){
        return "天安保险";
    }else if(val == "TPBX"){
        return "太平保险";
    }else if(val == "ZGRB"){
        return "中国人保";
    }else if(val == "ZGRS"){
        return "中国人寿";
    }else if(val == "ZGPA"){
        return "中国平安";
    }
}



//图片方向处理,安卓微信压缩问题hack,IOS6/7压缩扭曲,多张上传
/*var all_files = []; //全部图片
var arr=[]; //图片路径
var j = 1;  //base64转码
//图片上传
function uploadImgs(obj) {
    $(obj).on('change', function (e) {
        var i = 0,
            //tmp_files = e.target.files,
            notSupport = false;

        var files = e.target.files,
            len = files.length;

        /!*
        //去重
        for (var k = 0, len = tmp_files.length; k < len; k++) {
            var is_push = true;
            for (var m = 0, mLen = all_files.length; m < mLen; m++) {
                if (all_files[m].name == tmp_files[k].name) {
                    is_push = false;
                    break;
                }
            }
            if (is_push) {
                all_files.push(tmp_files[k]);
                files.push(tmp_files[k]);
            }
        }*!/

        // 循环多张图片，需要for循环和notSupport变量支持（检测）
        for (; i < len; i++) {
            if (files[i]) {
                if(!/image\/\w+/.test(files[i].type)){
                    mui.toast("请确保文件为图像类型");
                    return false;
                }

                if (!notSupport) {
                    (function(i) {
                        //图片方向处理,安卓微信压缩问题hack,IOS6/7压缩扭曲
                        new html5ImgCompress(files[i], {
                            before: function(file) {
                                //console.log('多张: ' + i + ' 压缩前...');
                            },
                            done: function (file, base64) { // 这里是异步回调，done中i的顺序不能保证
                                //console.log('多张: ' + i + ' 压缩成功...');
                                //insertImg(file, j); //处理前
                                insertImg(base64, j++);
                            },
                            fail: function(file) {
                                //console.log('多张: ' + i + ' 压缩失败...');
                            },
                            complate: function(file) {
                                //console.log('多张: ' + i + ' 压缩完成...');
                            },
                            notSupport: function(file) {
                                notSupport = true;
                                alert('浏览器不支持！');
                            }
                        });
                    })(i);
                }
            }
        }
        //console.log(arr);
    });
}


//插入图片
function insertImg(file, j) {
    var img = new Image(),
        title, src, size, base;

    if (typeof file === 'object') {
        title = '前';
        size = file.size;
        src = URL.createObjectURL(file);
        base = 1024;
    } else {
        title = '后';
        size = file.length;
        src = file;
        base = 1333;
    }

    //src = file;
    var li=$('<li class="mui-table-view-cell mui-media mui-col-xs-6"></li>');
    img.onload = function() {
        $("<img class='mui-media-object' src="+src+" data-preview-src='' data-preview-group='1'/><div class='mui-media-body' onclick=$(this).prev().parent().remove();delectImg('"+src+"');>删除</div>").appendTo(li);
        console.log('图片' + j + '，处理' + title + '质量约：' + (size / base).toFixed(2) + 'KB' + '，尺寸：' + this.width + '*' + this.height);
    };
    $("#imgList").append(li);
    img.src = src;
    arr.push(src);
    file = null; // 处理完清缓存
};*/

//删除图片
function delectImg(file) {
    removeByValue(arr, file);
}

//数组中删除指定值元素
function removeByValue(arr, val) {
    for(var i=0; i<arr.length; i++) {
        if(arr[i] == val) {
            arr.splice(i, 1);
            break;
        }
    }
}

//将base64转为Blob
function dataURItoBlob(dataUrl) {
    var byteString = atob(dataUrl.split(',')[1]);
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }
    return new Blob([ab], { type: 'image/jpeg' });
}

//blob转成base64
var blobToBase64 = function(blob, callback) {
    var a = new FileReader();
    a.onload = function(e) {
        callback(e.target.result);
    }
    a.readAsDataURL(blob);
};
//使用方法,传入一个blob数据,在回调中接收处理成功的图片url
/*
blobToBase64(formdata.get("uploadFileList"), function(data) {
    $('#successimg img').attr('src', data);
});*/